package com.ruoyi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.domain.Course;
import com.ruoyi.domain.Student;
import com.ruoyi.domain.Teacher;
import com.ruoyi.domain.TeacherEvaluation;
import com.ruoyi.service.ICourseService;
import com.ruoyi.service.ITeacherService;
import com.ruoyi.service.impl.TeacherEvaluationServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 教师Controller
 *
 * @author ruoyi
 * @date 2024-04-22
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController extends BaseController {
    @Autowired
    private ITeacherService teacherService;
    @Autowired
    TeacherEvaluationServiceImpl teacherEvaluationService;
    @Autowired
    private ICourseService iCourseService;

    /**
     * 查询教师列表
     */

    @GetMapping("/list")
    public TableDataInfo list(Teacher teacher) {
        startPage();
        List<Teacher> list = teacherService.selectTeacherList(teacher);
        return getDataTable(list);
    }
    @GetMapping("/getTeacher/{studentId}")
    public AjaxResult getTeacher(@PathVariable("studentId")Long studentId){
        startPage();
        List<Teacher> teachers =  teacherService.getTeacher(studentId);
        TableDataInfo dataTable = getDataTable(teachers);
        return AjaxResult.success(dataTable);
    }

    @PostMapping("/rateTeach/{student_Id}/{teach_id}/{score}")
    public AjaxResult rateTeach(@PathVariable("student_Id") Long student_Id, @PathVariable("teach_id") Long teach_id, @PathVariable("score") String score) {
        teacherEvaluationService.insertTeacherEvaluation(new TeacherEvaluation(student_Id, teach_id, score));
        return AjaxResult.success();
    }

    @GetMapping("/getTeacherRate")
    public AjaxResult getAllTeacherRate(@RequestParam(value = "name",required = false) String teacherName){
        startPage();
        List<Teacher> teachers = teacherService.getAllTeacherRate(teacherName);
        return AjaxResult.success(getDataTable(teachers));
    }
    @GetMapping("/getTeacherRate/{teachId}/{studentId}")
    public AjaxResult getTeacherRate(@PathVariable("teachId")Long teachId,@PathVariable("studentId")Long studentId){
        TeacherEvaluation teacherEvaluation= teacherService.getTeacherRate(teachId,studentId);
        return AjaxResult.success(teacherEvaluation);
    }

    @GetMapping("getCourse/{teachId}")
    public AjaxResult getCourse(@PathVariable("teachId") Long teachId) {
        startPage();
        List<Course> courses = teacherService.getCourseByTeacherId(teachId);
        return AjaxResult.success(getDataTable(courses));
    }

    @GetMapping("/getStudent/{teachId}/{courseId}")
    public AjaxResult getStudent(@PathVariable("teachId") Long teachId,@PathVariable("courseId") Long courseId,@RequestParam(value = "courseName",required = false)String courseName,@RequestParam(value = "studentName",required = false)String studentName) {
        startPage();
        List<Student> student = teacherService.getStudent(teachId, courseId, courseName, studentName);
        return AjaxResult.success(getDataTable(student));
    }

    /**
     * 导出教师列表
     */

    @Log(title = "教师", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Teacher teacher) {
        List<Teacher> list = teacherService.selectTeacherList(teacher);
        ExcelUtil<Teacher> util = new ExcelUtil<Teacher>(Teacher.class);
        util.exportExcel(response, list, "教师数据");
    }

    @GetMapping("getTeachList")
    public AjaxResult getTeachList(){
        List<Teacher> teachers = teacherService.getAllTeach();
        return AjaxResult.success(teachers);
    }
    /**
     * 获取教师详细信息
     */

    @GetMapping(value = "/{teacherId}")
    public AjaxResult getInfo(@PathVariable("teacherId") Long teacherId) {
        return success(teacherService.selectTeacherByTeacherId(teacherId));
    }

    /**
     * 新增教师
     */

    @Log(title = "教师", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Teacher teacher) {
        return toAjax(teacherService.insertTeacher(teacher));
    }

    /**
     * 修改教师
     */

    @Log(title = "教师", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Teacher teacher) {
        return toAjax(teacherService.updateTeacher(teacher));
    }

    /**
     * 删除教师
     */

    @Log(title = "教师", businessType = BusinessType.DELETE)
    @DeleteMapping("/{teacherIds}")
    public AjaxResult remove(@PathVariable Long[] teacherIds) {
        return toAjax(teacherService.deleteTeacherByTeacherIds(teacherIds));
    }
}
